package com.xywy.wjk;

import java.util.Currency;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothManager;
import android.bluetooth.BluetoothProfile;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.app.MainApplication;
import com.xywy.common.bean.ConnectedDevice;
import com.xywy.common.util.FileUtil;
import com.xywy.common.widget.TitleBar2;
import com.xywy.wjk.util.WjkConstant;
import com.xywy.wjk.widget.TipDialog;
import com.xywy.wjk.widget.TipDialog.CancleListener;
import com.xywy.wjk.widget.TipDialog.ConfirmListener;
import com.xywy.wjk.widget.TipDialog.OkayListener;

public class SelectBrandActivity extends BaseActivity implements
		OnClickListener {
	private TitleBar2 titleBar;
	private TextView tvTitle;
	private TextView tvBack;
	private TextView tvGo;
	private TextView tv_device;
	private TextView tv_KBB;
	private String deviceType;
	private ImageView iv_choose; // 选中按钮
	private ConnectedDevice current_device;
	
	private BluetoothAdapter mBluetoothAdapter;
	private BluetoothManager mBluetoothManager;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_selected_brand);
		mBluetoothManager = (BluetoothManager) context
				.getSystemService(Context.BLUETOOTH_SERVICE);
		mBluetoothAdapter = mBluetoothManager.getAdapter();
		this.initView();
		this.preInit();
		MainApplication.addSpeciall(this);
	}

	final int WEIGHT_SCALE = 0; // 当前状态体重秤
	final int BLOOD_PRESURE = 1;// 当前状态血压仪
	final int BLOOD_SUGAR = 2;// 当前状态血糖
	final int TEMP_ = 3; // 当前状态温度计

	int cur_device = -1; // 表示当前设备
	private String type;

	@Override
	public void preInit() {
		initBLE();
		current_device = FileUtil.getCurrentDevice();
		System.out.println(current_device.getDevice_name());
		Intent intent = getIntent();
		if (intent.hasExtra("deviceType")) {
			deviceType = intent.getStringExtra("deviceType");
			tv_KBB.setText("康宝贝" + deviceType);
			if (deviceType.equals("体重秤")) {
				tv_KBB.setText("康宝贝体质分析仪");
				cur_device = WEIGHT_SCALE;
				/* 控制展示当前设备是否选中 */
				if (current_device != null && current_device.isConnected() && ConnectedDevice.WEIGHT_NAME.equals(current_device.getDevice_name())) {
					iv_choose.setVisibility(View.VISIBLE);
					tv_KBB.setClickable(false);
				} else {
					tv_KBB.setClickable(true);
				}
			} else if (deviceType.equals("血压仪")) {
				cur_device = BLOOD_PRESURE;
				/* 控制展示当前设备是否选中 */
				if (current_device != null && current_device.isConnected() && ConnectedDevice.BLOOD_NAME.equals(current_device.getDevice_name())) {
					iv_choose.setVisibility(View.VISIBLE);
					tv_KBB.setClickable(false);
				} else {
					tv_KBB.setClickable(true);
				}
			} else if (deviceType.equals("血糖仪")) {
				cur_device = BLOOD_SUGAR;
			} else if (deviceType.equals("体温计")) {
				cur_device = TEMP_;
				if (current_device != null && current_device.isConnected() && ConnectedDevice.TEMPUTER_NAME.equals(current_device.getDevice_name())) {
					iv_choose.setVisibility(View.VISIBLE);
					tv_KBB.setClickable(false);
				} else {
					tv_KBB.setClickable(true);
				}
			}

		} else {
			Log.d(TAG, "none extra");
			return;
		}

	}

	/**
	 * 初始化蓝牙配置
	 */
	private void initBLE() {
		MainApplication app = MainApplication.getInstance();
		app.addSpeciall(this);
	}

	@Override
	public void loadData() {

	}

	@Override
	public void initView() {
		titleBar = (TitleBar2) findViewById(R.id.activity_device_type_add_title);
		tvTitle = titleBar.getTitle();
		tvTitle.setText(String.valueOf(getIntent().getExtras()
				.get("deviceType")));
		tvBack = titleBar.getTvBack();
		tvBack.setText(getString(R.string.tv_start_back));
		tvBack.setOnClickListener(this);
		tvGo = titleBar.getGo();
		tvGo.setVisibility(View.GONE);

		tv_device = (TextView) findViewById(R.id.tv_device);
		type = String.valueOf(getIntent().getExtras().get(
				"deviceType"));
		tv_device.setText(type);

		tv_KBB = (TextView) findViewById(R.id.tv_KBB);
		tv_KBB.setOnClickListener(this);
		iv_choose = (ImageView) findViewById(R.id.iv_choose);
	}

	@Override
	public void onClick(View view) {
//		System.out.println("康宝贝设备连接，关闭当前页面，现在跳转到哪里了？");
		switch (view.getId()) {
		case R.id.tv_title_back:
			this.finish();
			break;
		case R.id.tv_KBB:
			if (!mBluetoothAdapter.isEnabled()) {
				unOpenBleTip();
				return;
			}
			// 确认连接当前设备对话框
			final TipDialog dialogSwitch = new TipDialog(
					SelectBrandActivity.this, TipDialog.VIEW_DIALOG);
			dialogSwitch.setTitleAndContent("连接设备", "确认连接到该设备吗？");
			dialogSwitch.setConfirmCallback(new ConfirmListener() {

				@Override
				public void confirm() {
					dialogSwitch.dismiss();
					current_device = FileUtil.getCurrentDevice();
					//蓝牙未开启
					if(current_device.isConnected()){
						if(cur_device ==WEIGHT_SCALE ){
							Intent intent = new Intent(WjkConstant.CONNECT_WEIGHT_ACTION_DISCONNECT);
							sendBroadcast(intent);
							MainApplication.finishSpeciall();
						}else if(cur_device == BLOOD_PRESURE){
							Intent intent = new Intent(WjkConstant.CONNECT_BLOOD_ACTION_DISCONNET);
							sendBroadcast(intent);
							MainApplication.finishSpeciall();
						}else if (cur_device == BLOOD_SUGAR) {
							Toast.makeText(SelectBrandActivity.this,
									"暂无血糖设备", Toast.LENGTH_LONG).show();
						}else if(cur_device == TEMP_){
							Intent intent = new Intent(WjkConstant.CONNECT_TEMPUT_ACTION_DISCONNECT);
							sendBroadcast(intent);
							MainApplication.finishSpeciall();
//							Toast.makeText(SelectBrandActivity.this,
//									"暂无体温设备", Toast.LENGTH_LONG).show();
						}
					}else {
						if(cur_device ==WEIGHT_SCALE ){
							Intent intent = new Intent(WjkConstant.CONNECT_WEIGHT_ACTION);
							sendBroadcast(intent);
							MainApplication.finishSpeciall();
						}else if(cur_device == BLOOD_PRESURE){
							Intent intent = new Intent(WjkConstant.CONNECT_BLOOD_ACTION);
							sendBroadcast(intent);
							MainApplication.finishSpeciall();
						}else if (cur_device == BLOOD_SUGAR) {
							Toast.makeText(SelectBrandActivity.this,
									"暂无血糖设备", Toast.LENGTH_LONG).show();
						}else if(cur_device == TEMP_){
//							Toast.makeText(SelectBrandActivity.this,
//									"暂无体温设备", Toast.LENGTH_LONG).show();
							Intent intent = new Intent(WjkConstant.CONNECT_TEMPUT_ACTION);
							sendBroadcast(intent);
							MainApplication.finishSpeciall();
						}
					}
					
				}
			});
			dialogSwitch.setCancleCallback(new CancleListener() {

				@Override
				public void cancle() {
					dialogSwitch.dismiss();
				}
			});
			dialogSwitch.show();

			break;
		}

	}
	public static final String EXTRA_DEVICE_NAME = "device_name";
	public static final String WEIGHT_SCALE_STARTNAME = "Xun"; // 体重秤开头名称：
	public static final String BLOOD_PRESURE_STARTNAME = "KBB"; // 血压计开头名称：
	public static final int SELECTBRANDACTIVITY_RESULT_CODE = 1010;// 放当前页面的结果码；
	
	/**
	 * 当前为打开蓝牙设备，
	 */
	private void unOpenBleTip() {
		final TipDialog dialog = new TipDialog(this, TipDialog.VIEW_TIP);
		dialog.setTitleAndContent("蓝牙未开启", "检测到你没有打开手机蓝牙，请打开后再试");
		dialog.setOkayCallback(new OkayListener() {

			@Override
			public void okay() {
				dialog.dismiss();
				Log.d(TAG, "用户未开启当前手机的蓝牙设备；");
			}
		});
		dialog.show();
	}
}
